
import pymysql

class MyDB():
    ## 建立数据库连接
    def __init__(self):
        self.con = pymysql.connect(
            user = "root",
            password = "Admin@888",
            host = "192.168.1.9",
            database = "brtc_api",
            port = 3306,
            autocommit = True
        )
        # 创建游标
        self.cursor = self.con.cursor()

    ## 查询一条数据
    def get_one(self,sql):
        # 执行SQL
        self.cursor.execute(sql)
        # 返回一条数据
        return self.cursor.fetchone()

    ## 执行DML语句
    def execute_sql(self,sql):
        # 执行sql
        self.cursor.execute(sql)
        # 提交数据
        self.con.commit()

    ## 释放数据库连接
    def __del__(self):
        self.cursor.close()
        self.con.close()


## 实例化MyDB()
mydb = MyDB()